package com.emip.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.emip.bean.RoleMenuBean;
import com.emip.config.CommonOperation;
import com.emip.dao.BaseDAO;
import com.emip.dao.RoleMenuDAO;

@Repository("roleMenuDAO")
public class RoleMenuDAOImpl extends BaseDAO implements RoleMenuDAO {

	public int add(RoleMenuBean entity) {
		Integer id = (Integer) this.getHibernateTemplate().save(entity);
		return id.intValue();
	}

	public List<String> l1MenuTobeAdd(int roleID) {
		String sql = "select distinct a.parentid from t_menu_def a, t_role_menu b where a.status=1 and a.id=b.menu_id and b.role_id="+roleID;
		PreparedStatement st = null;
		Connection conn = this.getHibernateTemplate().getSessionFactory().openSession().connection();
		List<String> result = new ArrayList<String>();
		try {
			st=conn.prepareStatement(sql);
			ResultSet rs = st.executeQuery();			
			while(rs.next()) {
				result.add(rs.getString(1));
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			try {
				if (st != null)
					st.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				System.out.println(e.toString());
			}
		}	
        return result;
	}

	public void deleteRoleMenu(int roleID) {
		String sql = "delete from T_Role_Menu where role_ID="+roleID;
		CommonOperation.getInstance().executeSQL(this.getHibernateTemplate().getSessionFactory().openSession().connection(), sql);
	}
}
